package com.huluobo.a2212mdmdemo

import android.os.Bundle
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.huluobo.a2212mdmdemo.pages.MainPage
import com.huluobo.a2212mdmdemo.pages.WelcomePage
import com.huluobo.a2212mdmdemo.ui.theme._2212MDMDemoTheme
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestPermissions(
            arrayOf(
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.ACCESS_FINE_LOCATION
            ), 102
        )
        setContent {
            _2212MDMDemoTheme {
                // A surface container using the 'background' color from the theme
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                    var startPage = "welcome"
                    val mmkv = MMKV.defaultMMKV()
                    val navHostController = rememberNavController()

                    //通过mmkv去读取是否之前进入过app,如果读取不到,默认就是false
                    val isIn = mmkv.decodeBool("isIn", false)
                    if (isIn) {
                        //如果进入过app,则直接进入到mainPage
                        startPage = "main"
                    } else {
                        //开启协程
                        lifecycleScope.launch {
                            //延时5秒
                            async { delay(5000) }.await()
                            //5秒过后进入到首页
                            navHostController.navigate("main")
                            //保存已经进入过app的key
                            mmkv.encode("isIn", true)
                        }
                    }

                    NavHost(navController = navHostController, startDestination = startPage, builder = {
                        composable("welcome") { WelcomePage() }
                        composable("main") { MainPage() }
                    })
                }
            }
        }
    }
}
